<?php
use \Entities;
use \Managers;

class TestController extends Zend_Controller_Action
{

    public function init() {
        
    }
    
    public function indexAction(){
        
        
        $def = array(
                 array('mark'=>"BMW", 'model'=>'Five (5)','born'=>1222)
                ,array('mark'=>"Audi", 'model'=>'100','born'=>1553)
                ,array('mark'=>"VF", 'model'=>'Beatle','born'=>1231)
            
                );
        
         $titleCar=array(   //в последствии брать с бд и отдавать объектно
               "mark%"
             , "model"
             , "born^ г.в."
             , "кпп|type"
             );
         
         $this->view->def = $def;
         
         $this->view->def2 = \Engine\Functions\parseAndConvert::keysHarvester2($titleCar, $def, ', ', ' ');
         
         
         
    }
    
    public function surfdbAction(){
        
        
       $params = array('host'=>'Имя Хоста (+порт)', 'user'=>'Имя пользователя', 'pass'=>'Пароль ');
       $this->view->allGettedParams = $this->_getAllParams();

       $inputForm = new Zend_Form();

       foreach($params as $elName=>$description){
           
           $tmpElement = new Zend_Form_Element_Text($elName);
           $tmpElement->clearDecorators();
           
           $tmpElement->addDecorator('Errors');
           $tmpElement->addDecorator('ViewHelper');
           
           $tmpElement->addDecorator(array('div1' => 'HtmlTag'), array('tag' => 'div', 'class' => 'elementRow', 'id' => 'error1'));
           $tmpElement->addDecorator(array('div2' => 'HtmlTag'), array('tag' => 'span', 'class' => 'errors'));
           
           $tmpElement->addDecorator('Description', array('tag' => 'p', 'class' => 'description'));
           $tmpElement->setRequired(true);
           
           if ($this->_request->isPost()) {
               $tmpElement->setValue($this->_getParam($elName));
           }
           $inputForm->addElement($tmpElement);
       }
       
       $submitToServer = new Zend_Form_Element_Submit('submitToServer');
       $submitToServer->setLabel('GO');
       $inputForm->addElement($submitToServer);
       
   

       $err = new Zend_Form_Decorator_Errors();
       
       $this->view->errors = $err;
       
//       $inputForm->setDecorators(array($err));
       
        
       
       if ($this->_request->isPost() && $inputForm->isValid($this->_getAllParams())) {
           $params = $this->_getAllParams();
           $this->dataAlreadyInput = $this->_getAllParams();
           $connect = mysql_connect($params['host'], $params['user'], $params['pass']);
           if (!$link) {
                $this->view->mysqlError =  'Could not connect: '.mysql_error();
           }else{
               $allDataBases = array();
               while ($row = mysql_fetch_object($connect)) {
                  $allDataBases[]=$row->Database;
               }
               $this->view->allDataBases = $allDataBases;
           }
           
           $this->_redirect($this->view->url(array(), 'surfdb'));
           
           
           
           
       }
       
//       $inputForm->setDecorators(array(array('ViewScript', array('viewScript' => 'test/forms/surfdb.phtml'))));
       $this->view->form = $inputForm;
       
    } 
    
}

